﻿using System;
using RedDockNet_BackEnd.Config;
using StackExchange.Redis;

namespace RedDockNet_BackEnd.Service
{
    public class AuthCodeService : IRedisService
    {
        private readonly ConnectionMultiplexer redis;
        private readonly IDatabase database;

        public AuthCodeService(IRedisConfig config)
        {
            ConfigurationOptions options = ConfigurationOptions.Parse(config.ConnectionString);
            options.DefaultDatabase = config.Database;
            this.redis = ConnectionMultiplexer.Connect(options);
            this.database = redis.GetDatabase();
        }

        public RedisValue GetKey(string key)
        {
            return database.StringGet(key);
        }

        public bool SetKey(string key, string value, TimeSpan? expiry = default)
        {
            return database.StringSet(key, value, expiry);
        }

        public bool ExistsKey(string key)
        {
            return database.KeyExists(key);
        }

        public bool DeleteKey(string key)
        {
            return database.KeyDelete(key);
        }
    }
}
